<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" v-if="permission.maintenanceRecord_view" @click="rowView(row)">详情</el-button>
        <el-button size="small" type="text" v-if="!permission.maintenanceRecord_logOff" @click="handleLogOff(row)" :disabled="row.status == 3">注销</el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
    </avue-crud>
    <el-dialog ref="dialog" title="详情" append-to-body :visible.sync="showDialog" :close-on-click-modal="false" top="5vh" height="80%">
      <avue-form ref="basicInfo" v-if="showDialog" v-model="form" :option="dialogOption">
        <template slot="attachment">
          <div v-if="pictureUrlList.length > 0">
            <div style="display: flex">
              <div v-for="(item, index) in pictureUrlList" :key="index">
                <el-image style="width: 100px; height: 100px; padding-right: 10px; display: block" :src="item.link" @click="openPreview(0, item.link)"></el-image>
                <el-button type="text" icon="el-icon-download" size="small" @click="handleDownload(item.link)">下载</el-button>
              </div>
            </div>
          </div>
          <div v-else></div>
        </template>
      </avue-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-product/equipmentmaintenance/page" :option="option" :page="page" :query="query" file-name="校验记录"> </export-comp>
  </basic-container>
</template>
<script>
import { getList, getDetail, cancel, getInspectionItemattributeList } from '@/api/equipment-manage/maintenance-records';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
export default {
  components: {
    exportComp
  },
  data() {
    return {
      form: {},
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        menuWidth: 200, //弹框内字段宽度属性
        dialogWidth: 880, //弹框宽度属性
        labelWidth: '110',
        searchLabelWidth: 130,
        dialogClickModal: false,
        column: [
          {
            label: '执行类型',
            prop: 'sourceType',
            disabled: true,
            dicData: [
              { label: '校验计划', value: 'PLAN' },
              { label: '临时校验', value: 'TEMP' }
            ]
          },
          {
            label: '校验计划编码',
            prop: 'planCode',
            disabled: true,
            search: true
          },
          {
            label: '校验计划名称',
            prop: 'planName',
            disabled: true
          },
          {
            label: '设备编码',
            prop: 'equipmentCode',
            disabled: true,
            search: true
          },
          {
            label: '设备名称',
            prop: 'equipmentName',
            disabled: true
          },
          {
            label: '状态',
            prop: 'status',
            disabled: true,
            search: true,
            type: 'select',
            dicData: [
              {
                label: '待执行',
                value: 1
              },
              {
                label: '已完成',
                value: 2
              },
              {
                label: '已注销',
                value: 3
              }
            ]
          }
        ]
      },
      showDialog: false,
      dialogOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: '130',
        group: [
          {
            label: '基本数据',
            arrow: false,
            prop: 'baseInfo',
            labelWidth: 100,
            display: true,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '设备名称',
                prop: 'equipmentName',
                disabled: true
              },
              {
                label: '设备编码',
                prop: 'equipmentCode',
                disabled: true
              },
              {
                label: '校验计划编码',
                prop: 'planCode',
                disabled: true
              },
              {
                label: '校验计划名称',
                prop: 'planName',
                disabled: true
              },
              {
                label: '任务生成时间',
                prop: 'createTime',
                disabled: true
              },
              {
                label: '执行人员',
                prop: 'examineByName',
                disabled: true
              },
              {
                label: '执行时间',
                prop: 'examineDate',
                disabled: true
              }
            ]
          },
          {
            label: '校验结果',
            arrow: false,
            prop: 'reslutInfo',
            labelWidth: 100,
            display: true,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '校验结果',
                prop: 'examineResult',
                type: 'radio',
                span: 8,
                row: true,
                dicData: [
                  {
                    label: '正常',
                    value: 'OK'
                  },
                  {
                    label: '异常',
                    value: 'NG'
                  }
                ]
              },
              {
                label: '图片',
                prop: 'attachment',
                row: true
              },
              {
                label: '备注',
                prop: 'examineRemark',
                row: true,
                type: 'textarea',
                span: 8,
                minRows: 3,
                maxRows: 3
              }
            ]
          }
        ]
      },
      testTemplateMainResults: [],
      pictureUrlList: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission'])
  },
  methods: {
    handleLogOff(row) {
      this.$confirm('确定将选择数据注销?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return cancel(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      params.planType = 'VERIFY_CHECK_PLAN';
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    rowView(row) {
      this.showDialog = true;
      getDetail(row.id).then((res) => {
        this.form = res.data.data;
        this.pictureUrlList = this.form.attachmentList;
        // this._forEach(this.form.inspectionItemDetailVOList || [], (item) => {
        //   if (item.examineRealValue && item.examineRealValue != null) {
        //     item.examineRealValue = JSON.parse(item.examineRealValue);
        //   }
        // });
      });
    },
    handleDownload(link) {
      window.open(`${link}`);
    },
    openPreview(index = 0, link) {
      this.$ImagePreview([{ url: link }], index, {
        closeOnClickModal: false
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.inspection {
  height: 300px;
  overflow: auto;
  .inspectionItem {
    padding: 10px 0;
    font-size: 15px;
    border-bottom: 1px solid #cccccc;
  }
}
</style>
